//
//  LBRouterHandler+Service.m
//  LBRouter
//
//  Created by WuShiHai on 19/5/24.
//  Copyright (c) 2019 Lebbay. All rights reserved.
//

#import "LBRouterHandler+Native.h"
#import "LBRouterDefine.h"

const NSString * ServicePrePath = @"native/service";

static NSMutableArray<NSString *> *LBNavtieServiceRouterConfig;
NSMutableArray<NSString *> *MLGetNativeServiceRouterConfig(void)
{
    return LBNavtieServiceRouterConfig;
}

void MLRegisterNativeServiceRouter(NSString *);
void MLRegisterNativeServiceRouter(NSString *router)
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        LBNavtieServiceRouterConfig = [NSMutableArray new];
    });
    // Register router
    if (router) {
        [LBNavtieServiceRouterConfig addObject:router];
    }
}

@implementation LBRouterHandler (Native)

#pragma mark - Callback
+ (BOOL)isNativeCallbackRouter:(NSString *)matchPath{
    return [matchPath hasPrefix:@"native/callback/"];
}

+ (NSString *)nativeCallbackMethod:(NSString *)matchPath{
    return [matchPath stringByReplacingOccurrencesOfString:@"native/callback/" withString:@""];
}

#pragma mark - Native Method
+ (BOOL)isNativeServiceRouter:(NSString *)matchPath{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        LB_REGISTER_SERVICE_ROUTER(@"openApp")
        LB_REGISTER_SERVICE_ROUTER(@"openTel")
    });
    
    return [self router_service_isNativeServiceRouter:matchPath];
}

+ (NSString *)methodNameOfNative:(NSString *)matchPath {
    NSString *methodName = matchPath.lastPathComponent;
    
    if ([matchPath isEqualToString:[@"native" stringByAppendingPathComponent:methodName]]) {
        return methodName;
    }
    
    return nil;
}

+ (NSString *)methodNameOfNativeService:(NSString *)matchPath{
    NSString *methodName = matchPath.lastPathComponent;

    if ([matchPath isEqualToString:[ServicePrePath stringByAppendingPathComponent:methodName]]) {
        return methodName;
    }
    
    return nil;
}

+ (BOOL)router_service_isNativeServiceRouter:(NSString *)matchPath{
    NSArray *filterArray = [MLGetNativeServiceRouterConfig() copy];
    filterArray = [filterArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString *evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        if ([matchPath isEqualToString:[ServicePrePath stringByAppendingPathComponent:evaluatedObject]]) {
            return YES;
        }else{
            return NO;
        }
    }]];
    return filterArray.count > 0;
}

+ (void)callNativeService:(NSString *)matchPath
                 userInfo:(NSDictionary *)userInfo{
    NSString *methodName = matchPath.lastPathComponent;
    if (methodName.length > 0) {
        SEL selector = NSSelectorFromString([methodName stringByAppendingString:@":"]);
        if ([self respondsToSelector:selector]) {
            ((void (*)(id, SEL, NSDictionary *))[self methodForSelector:selector])(self, selector, userInfo);
        }
    }
}

+ (void)openApp:(NSDictionary *)userInfo{
    NSString *link = [userInfo objectForKey:@"url"];
    if ([link isKindOfClass:[NSString class]]) {
        NSURL *URL = [[NSURL alloc] initWithString:link];
        if (URL) {
            [[UIApplication sharedApplication] openURL:URL];
        }
    }
}

+ (void)openTel:(NSDictionary *)userInfo{
    NSString *tel = [userInfo objectForKey:@"tel"];
    tel = [NSString stringWithFormat:@"tel://%@",tel];
    NSURL *URL = [[NSURL alloc] initWithString:tel];
    if (URL && [[UIApplication sharedApplication] canOpenURL:URL]) {
        [[UIApplication sharedApplication] openURL:URL];
    }
}

@end
